package com.xhlj.controller;

import com.xhlj.entity.Post;
import com.xhlj.service.PostService;
import com.xhlj.utils.JwtHelper;
import com.xhlj.utils.PayLoad;
import com.xhlj.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("post")
public class PostController {
    @Resource
    private PostService postService;
    @Resource
    private JwtHelper helper;

    /*
     * 获取帖子列表(暂时先除掉所有的转发帖子)
     * */

    public Result getPostList() {
        Result res = postService.getPostList();
        return res;
    }

    /*
     * 分页获取全部帖子列表
     * */
    @GetMapping("list")
    public Result getPostPage(Integer pageSize, Integer pageNum) {
        Result res = postService.getPostPage(pageSize, pageNum);
        return res;
    }

    /*
     * 根据用户ID获取自己的帖子
     * */
    @GetMapping("userPost")
    public Result getUserPost(@RequestHeader String Token) {
        PayLoad payLoad = helper.getPayLoad(Token);
        Integer userId = payLoad.getUserId();
        Result res = postService.getUserPost(userId);
        return res;
    }

    /*
     * 添加帖子
     * */
    @PostMapping("create")
    public Result createPost(@RequestBody Post post) {
        Result res= postService.createPost(post);
        return res;
    }

    /*
     * 删除帖子(只能删除自己的),   todo:要去redis里那些即将删掉的评论的点赞信息,未读状态全删了
     * */
    @DeleteMapping
    public Result deletePost(Integer postId,@RequestHeader String Token) {
        Integer userId = helper.getPayLoad(Token).getUserId();
        Result res = postService.deletePost(postId,userId);
        return res;
    }

    /*
     * 帖子的转发,生成一个链接,前端可以拿到地址
     * */
    public Result sharePost() {

        return null;
    }


}
